package controller;
import controller.KeyListeners.*;
import model.playable.Playable;
import model.command.Command;
import model.Model;
import clock.Clock;
/**
 * Initiates the controller subsystem and provides an interface between the
 * other subsystems.
 *
 * @author joseph
 */
public class Controller {
    private Clock clock;
    private Playable currentPlayable;
    private static KeyListenerSet listeners;
    private Model model;

    public Controller(Clock clock, Model model){
        listeners = new KeyListenerSet(this);
        int listenTo[] = {0, 1, 5, 6};
        listeners.registerKLSet(listenTo);
        setClock(clock);
        setModel(model);
        setCurrentPlayable(model.getInitialPlayable());
    }
    /**
     * 
     */
    public void cycleCommandDown(){
    	if(currentPlayable != null) currentPlayable.getNextPossibleCommand().attachToListener();
		System.out.println("\t\tCYCLE COMMAND");
		try{
			System.out.println("\t\t\t" + currentPlayable.toString() + " " +currentPlayable.getCurrentPossibleCommand().toString());
		}catch(NullPointerException NPE){}
    }
       /**
     *
     */
    public void cycleCommandUp(){
        if(currentPlayable != null) currentPlayable.getPreviousPossibleCommand().attachToListener();
		System.out.println("\t\tCYCLE COMMAND");
		try{
			System.out.println("\t\t\t" + currentPlayable.toString() + " " +currentPlayable.getCurrentPossibleCommand().toString());
		}catch(NullPointerException NPE){}
    }
    /**
     *
     */
    public void cycleInstanceDown(){
        setCurrentPlayable(model.getNextInstance());
		System.out.println("\t\tCYCLE NEXT INSTANCE");
    }
    /**
     *
     */
    public void cycleInstanceUp(){
        setCurrentPlayable(model.getPrevInstance());
		System.out.println("\t\tCYCLE PREV INSTANCE");
    }
    /**
     * 
     */
   public void cycleInstance(int number){
       setCurrentPlayable(model.getInstance(number));
		System.out.println("\t\tCYCLE INSTANCE");
    }
    /**
     *
     */
    public void cycleSubTypeDown(){
        setCurrentPlayable(model.getNextSubtype());
		System.out.println("\t\tCYCLE NEXT SUBTYPE");
    }
    /**
     *
     */
    public void cycleSubTypeUp(){
        setCurrentPlayable(model.getPrevSubtype());
		System.out.println("\t\tCYCLE PREV SUBTYPE");
    }
    /**
     *
     */
    public void cycleTypeDown(){
        setCurrentPlayable(model.getNextType());
		System.out.println("\t\tCYCLE NEXT TYPE");
    }
    /**
     *
     */
    public void cycleTypeUp(){
        setCurrentPlayable(model.getPrevType());
		System.out.println("\t\tCYCLE PREV TYPE");
    }
    /**
     * 
     * @return
     */
    public String getCurrentCommandName(){
    	if(currentPlayable == null){}
        return currentPlayable.getCurrentPossibleCommand().toString();
    }
    /**
     *
     * @return
     */
    public Playable getCurrentPlayable(){
        return currentPlayable;
    }
    /**
     *
     * @return
     */
    public static KeyListenerSet getListeners(){
        return listeners;
    }
    /**
     *
     * @param command
     */
    public void queueCommandInPlayable(Command command){
        currentPlayable.addToCommandQueue(command);
    }
    /**
     *
     * @param clock
     */
    private void setClock(Clock clock){
        this.clock = clock;
    }
    /**
     *
     * @param currentPlayable
     */
    public void setCurrentPlayable(Playable currentPlayable){
    	System.out.println("\t\t\tAttempting to attach to controller");
    	if(currentPlayable != null){
    		this.currentPlayable = currentPlayable;
    		currentPlayable.getCurrentPossibleCommand().attachToListener();
    		System.out.println("\t\tNEW PLAYABLE");
    	}
    }
    /**
     * 
     * @param model
     */
    public void setModel(Model model){
        this.model = model;
    }
    /**
     *
     */
    public void startGame(){
        clock.start();
    }
}
